//
//  PermissionsDialogHelper.cpp
//  client
//
//  Created by ming on 2019/4/10.
//

#include "PermissionsDialogHelper.hpp"
#include "AppDelegate.h"
#include "MessageDialog.h"
#include "Localization.h"


PermissionsDialogHelper * PermissionsDialogHelper::create(const char * permissionsStr, const char * grantedFailStr, const char * grantedFailAndForbidStr, const PermissionsDialogHelperCallback & successCb){
    PermissionsDialogHelper * p = new PermissionsDialogHelper();
    p->setPermissionsStr(permissionsStr);
    p->setGrantedFailStr(grantedFailStr);
    p->setGrantedFailAndForbidStr(grantedFailAndForbidStr);
    p->setSuccessCallback(successCb);
    return p;
}


PermissionsDialogHelper::PermissionsDialogHelper():Ref()
{
    _grantedFailAndForbidStr = "";
    _grantedFailStr = "";
    _permissionsStr = "";
    _listener = nullptr;
    _successCallback = nullptr;
    _failCallback = nullptr;
   
}

PermissionsDialogHelper::~PermissionsDialogHelper(){
    _removeEventListen();
}

void PermissionsDialogHelper::_removeEventListen(){
    if(_listener != nullptr)
    {
        Director::getInstance()->getEventDispatcher()->removeEventListener(_listener);
        _listener = nullptr;
    }
}

void PermissionsDialogHelper::_addEventListen(){
    if(_listener == nullptr)
    {
        _listener = Director::getInstance()->getEventDispatcher()->addCustomEventListener("COCOS_REQUEST_PREMISSIONS_EVENT", [this](cocos2d::EventCustom* event){
            std::string param = event->getUserStr();
            int ret = atoi(param.c_str());
            if(ret == 0){
                _removeEventListen();
                if(_successCallback){
                    _successCallback(ret);
                }
            }else if (ret == 1){
                _removeEventListen();
                if(_failCallback){
                    _failCallback(ret);
                }else{
                    showGrantedFailDialog();
                }
                
            }else if (ret == 2){
                _removeEventListen();
                if(_failCallback){
                    _failCallback(ret);
                }else{
                    showGrantedFailAndForbidDialog();
                }
            }else if (ret == 3){
                // 已经有一个在申请权限了
            }else if (ret == 4){
                //其他错误
                _removeEventListen();
                if(_failCallback){
                    _failCallback(ret);
                }
            }
        });
    }
}


void PermissionsDialogHelper::requestPermissions(){
#if CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID
    _addEventListen();
    AppDelegate::requestPermissions(_permissionsStr.c_str());
#else
    _removeEventListen();
    if(_successCallback){
        _successCallback(0);
    }
#endif
    
}

void PermissionsDialogHelper::showGrantedFailDialog(){
    if(_grantedFailStr != ""){
        auto scene = Director::getInstance()->getRunningScene();
        if(scene){
            MessageDialog* dialog = MessageDialog::create();
            //    dialog->setTwoButton(true);
            dialog->setMessage(_grantedFailStr.c_str());
            dialog->setTwoButton(true);
            dialog->setConfirmButton(Localization::getInstance()->getStr("CommonBtnConfirm").c_str());
            dialog->setCancelButtonStr(Localization::getInstance()->getStr("CommonBtnCancel").c_str());
            dialog->setConfirmCallback([this](MessageDialog* sender){
                sender->removeFromParent();
                requestPermissions();
            });
            dialog->setCancelCallback([this](MessageDialog* sender){
                sender->removeFromParent();
            });
            scene->addChild(dialog, 1000);
        }
    }
}

void PermissionsDialogHelper::showGrantedFailAndForbidDialog(){
    if(_grantedFailAndForbidStr != ""){
        auto scene = Director::getInstance()->getRunningScene();
        if(scene){
            MessageDialog* dialog = MessageDialog::create();
            dialog->setMessage(_grantedFailAndForbidStr.c_str());
            dialog->setConfirmButton(Localization::getInstance()->getStr("CommonBtnAppSetting").c_str());
            dialog->setConfirmCallback([this](MessageDialog* sender){
                sender->removeFromParent();
                AppDelegate::goAppSetting();
            });
            scene->addChild(dialog, 1000);
        }
    }
}

